package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    //查看所有上师  分页查询
    @RequestMapping("/listGuru")
    public Map listGuru(int page,int limit,String name){
        return guruService.listGuru(page,limit,name);
    }

    //添加上师
    @RequestMapping("/insertGuru")
    public Map insertGuru(Guru guru, MultipartFile imageUrl, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("/img");
        //重命名，防止同名文件被覆盖
        String newFileName = new Date().getTime() + "_" + imageUrl.getOriginalFilename();
        imageUrl.transferTo(new File(realPath + "/" + newFileName));

        guru.setGuruImage("http://localhost:8989/cmfzdemo/img/"+imageUrl.getOriginalFilename());

        return guruService.insertGuru(guru);
    }

    //修改上市状态  冻结或者解冻
    @RequestMapping("/updateGuru")
    public Map updateGuru(Integer id,Integer status){
        System.out.println("id = " + id);
        System.out.println("status = " + status);
        return guruService.updateGuru(id,status);
    }

    //删除上师
    @RequestMapping("/deleteGuru")
    public Map deleteGuru(Integer id){
        return guruService.deleteGuru(id);
    }


    //批量上传上师  既批量添加--批量导入
    @RequestMapping("/multiInsertGuru")
    public void multiInsertGuru() throws IOException {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<Guru> listGuru= ExcelImportUtil.importExcel(
                new File("E://guru.xls"),
                Guru.class, params);

        for (Guru guru : listGuru) {
            //批量添加到数据库
            guruService.insertGuru(guru);
        }
    }

    //批量下载上师  批量导出
    @RequestMapping("/multiDeleteGuru")
    public void multiDeleteGuru() throws IOException, IllegalAccessException {

        ExportParams exportParams = new ExportParams("上师表数据", "guru");
        List<Guru> listGuru2 = guruService.listGuru2();
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,Guru.class, listGuru2);
        workbook.write(new FileOutputStream("E://guru.xls"));
    }

    //查询所有上师的法号，用于文章页面修改时选择上师，使用下拉框实现
    @RequestMapping("/selectAllGuruNick")
    public List<Guru> selectAllGuruNick(){
        return guruService.listGuruNick();
    }


}
